package com.sino.dataworks.util;

import com.sino.dataworks.dto.PersonVo;
import com.sino.dataworks.entity.DwDutyRoster;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DutyRosterUtil {

    public static List<DwDutyRoster> generateDutyRoster( String start, String end) {
        List<DwDutyRoster> dutyRosterList = new ArrayList<>();
        LocalDate startDate=LocalDate.parse(start);
        LocalDate endDate=LocalDate.parse(end);
        int totalDays = (int) java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
        List<PersonVo> firstShift=getDutyPerson("first.txt");
        List<PersonVo> secondShift=getDutyPerson("second.txt");
        List<PersonVo> thirdShift=getDutyPerson("third.txt");
        int firstIndex = 0;
        int secondIndex = 0;
        int thirdIndex = 0;

        for (int day = 0; day < totalDays; day++) {
            LocalDate dutyDate = startDate.plusDays(day);
            DwDutyRoster dutyRoster = new DwDutyRoster();
            dutyRoster.setDataDt(dutyDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));

            PersonVo firstPerson = firstShift.get(firstIndex);
            dutyRoster.setFirstPhone(firstPerson.getPhone());
            dutyRoster.setFirstChargePerson(firstPerson.getName());
            if ((day + 1) % 7 == 0) {
                firstIndex = (firstIndex + 1) % firstShift.size();
            }

            PersonVo secondPerson = secondShift.get(secondIndex);
            dutyRoster.setSecondPhone(secondPerson.getPhone());
            dutyRoster.setSecondChargePerson(secondPerson.getName());
            if ((day + 1) % 7 == 0) {
                secondIndex = (secondIndex + 1) % secondShift.size();
            }

            PersonVo thirdPerson = thirdShift.get(thirdIndex);
            dutyRoster.setThirdPhone(thirdPerson.getPhone());
            dutyRoster.setThirdChargePerson(thirdPerson.getName());
            if ((day + 1) % 7 == 0) {
                thirdIndex = (thirdIndex + 1) % thirdShift.size();
            }

            dutyRosterList.add(dutyRoster);
        }

        return dutyRosterList;
    }

  private static List<PersonVo> getDutyPerson(String fileName) {
        List<PersonVo> dutyPerson=new ArrayList<>();
      Stream<String> stream=null;
        try {
            stream=Files.lines(Paths.get(fileName));
            List<String> list= stream.collect(Collectors.toList());
            for (String s:list){
                String[] split=s.split("\\s+");
                PersonVo personVo=new PersonVo();
                personVo.setName(split[0]);
                personVo.setPhone(split[1].trim());
                dutyPerson.add(personVo);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(stream!=null){
                stream.close();
            }
        }
        return dutyPerson;
  }
}






